package com.probiz.estore.system.dao.impl;

import java.util.List;

import com.probiz.estore.system.dao.MsgTemplateDao;
import com.probiz.estore.common.model.system.MsgTemplate;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;

/**
 * Dao implementation for MsgTemplate.
*/
public class MsgTemplateDaoImpl extends HibernateGenericDaoImpl<MsgTemplate> implements MsgTemplateDao {
	@Override
	protected void initFilterHqlMap() {
		//filterHqlMap.put(MsgTemplate.FilterHqlName.DEFAULT, "from MsgTemplate mt where mt.store.storeId IS NULL order by mt.msgTemplateId asc");
		filterHqlMap.put(MsgTemplate.FilterHqlName.DEFAULT, "from MsgTemplate mt where mt.storeId IS NULL order by mt.msgTemplateId asc");
		//filterHqlMap.put(MsgTemplate.FilterHqlName.DEFAULT_BY_STORST, "from MsgTemplate mt where mt.store.storeId=? order by mt.msgTemplateId asc");
		filterHqlMap.put(MsgTemplate.FilterHqlName.DEFAULT_BY_STORST, "from MsgTemplate mt where mt.storeId is null order by mt.msgTemplateId asc");
	}
	
	public MsgTemplate getByTemplate(Integer storeId, String templateCode) {
		//String hql = "from MsgTemplate mt where mt.store.storeId =? and mt.templateCode=? ";
		String hql = "from MsgTemplate mt where  mt.templateCode=? ";
		List result = this.findByHql(hql, storeId, templateCode);;
		if (result != null && result.size() > 0) {
			return (MsgTemplate) result.get(0);
		} else {
			return null;
		}
	}

	public MsgTemplate getDefaultTemplate( String templateCode) {
		//String hql = "from MsgTemplate mt where mt.store.storeId is null and mt.templateCode=? ";
		String hql = "from MsgTemplate mt where mt.storeId is null and mt.templateCode=? ";
		List result = this.findByHql(hql, templateCode);
		if (result != null && result.size() > 0) {
			return (MsgTemplate) result.get(0);
		} else {
			return null;
		}
	}

	public List<MsgTemplate> getByTemplate(Integer storeId, Integer templateItem, Short type) {
		//String hql = "from MsgTemplate mt where mt.store.storeId=? and mt.templateItem =? and mt.type=? ";
		String hql = "from MsgTemplate mt where  mt.templateItem =? and mt.type=? ";
		return this.findByHql(hql, storeId, templateItem, type);
	}
	
	public List<MsgTemplate> getGroupByTemplate(Integer storeId, Integer isStoreAdmin){
		StringBuffer hql = new StringBuffer();
		hql.append("SELECT ");
		hql.append(" new map(mt.templateName as templateName,  mt.templateItem as templateItem, mt.isStoreAdmin as isStoreAdmin, ");
		hql.append(" max(CASE WHEN mt.type=0 THEN mt.msgTemplateId ELSE 1 END) AS msgTemplateId0, ");
		hql.append(" max(CASE WHEN mt.type=1 THEN mt.msgTemplateId ELSE 2 END) AS msgTemplateId1, ");
		hql.append(" max(CASE WHEN mt.type=2 THEN mt.msgTemplateId ELSE 3 END) AS msgTemplateId2, ");
		hql.append(" max(CASE WHEN mt.type=0 THEN mt.status END) AS status0, ");
		hql.append(" max(CASE WHEN mt.type=1 THEN mt.status END) AS status1, ");
		hql.append(" max(CASE WHEN mt.type=2 THEN mt.status END) AS status2, ");
		hql.append(" max(CASE WHEN mt.type=0 THEN mt.type END) AS type0, ");
		hql.append(" max(CASE WHEN mt.type=1 THEN mt.type END) AS type1, ");
		hql.append(" max(CASE WHEN mt.type=2 THEN mt.type END) AS type2) ");
		hql.append(" FROM MsgTemplate as mt");
//		if(null != isStoreAdmin && isStoreAdmin.equals(1)){
//			hql.append(" LEFT JOIN mt.store as st WHERE 0=0 and mt.isStoreAdmin = 1");
//		}else{
//			hql.append(" LEFT JOIN mt.store as st WHERE 0=0 ");
//		}
		hql.append(" WHERE 0=0 and mt.isStoreAdmin = 1");
		
		
//		if (storeId == null) {
//			hql.append(" And st.storeId IS NULL");
//		} else {
//			hql.append(" And st.storeId= ?");
//		}
		
		hql.append(" GROUP BY mt.templateName,  mt.templateItem, mt.isStoreAdmin");
		
		hql.append(" ORDER BY mt.msgTemplateId desc");
		
//		if (storeId == null) {
//			return this.findByHql(hql.toString());
//		} else {
//			return this.findByHql(hql.toString(), storeId);
//		}
		return this.findByHql(hql.toString());
	}

	public List<MsgTemplate> getDefaultMsgTemplateList(Integer templateItem) {
		//String hql = "select m from MsgTemplate m where m.store.storeId is null and m.templateItem = ?";
		String hql = "select m from MsgTemplate m where m.storeId is null and m.templateItem = ?";
		return this.findByHql(hql,  templateItem);
	}

	public List<MsgTemplate> getMsgTemplateList(Integer storeId,
			Integer templateItem) {
		//String hql = "select m from MsgTemplate m where m.store.storeId = ? and m.templateItem = ?";
		String hql = "select m from MsgTemplate m where  m.templateItem = ?";
		return this.findByHql(hql, storeId, templateItem);
	}

	public List<MsgTemplate> getMsgTemplateList(Integer storeId) {
		StringBuffer hql = new StringBuffer();
		hql.append("select m from MsgTemplate m where 1 = 1");
//		if (storeId != null) {
//			hql.append(" and m.store.storeId =" + storeId);
//		} else {
//			hql.append(" and m.store.storeId is null");
//		}
		hql.append(" and m.storeId is null");
		return this.findByHql(hql.toString());
	}
}
